package vg.modules.opener.system;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import vg.core.AModel;
import vg.core.VisualGraph;
import vg.core.exception.CoreException;
import vg.core.exception.EnumCriticalityException;
public class SimpleOpener {
/**
* This field mapped extension to decoder
*/
private HashMap <String,IDecoder> decoders;
private ArrayList<String> openedFilesList;
public SimpleOpener() {
decoders = new HashMap<String, IDecoder>();
openedFilesList = new ArrayList<String>();
//register decoders TODO move this stuff into method
decoders.put("graphml", new GraphMLDecoder());
decoders.put("gml", new GMLDecoder());
}
/**
* This method reads a graph from a graphML file into this.model
* @param fileName - name of file to be read
* @return - Number of read graph in the model
* @throws CoreException
*/
public int openFile(String fileName, AModel model) throws CoreException{
//getting decoder for file
String extension = fileName.toLowerCase().replaceAll(".*\\.", "");
IDecoder decoder = decoders.get(extension);
if (decoder == null)
throw new CoreException("Couldn't find decoder for *." + extension + "file",
EnumCriticalityException.ERROR);
String name = "Unknown";
int index = fileName.lastIndexOf(File.separatorChar);
if (index < 0) {
name = fileName;
} else {
name = fileName.substring(index + 1);
}
Date d = new Date();
Integer graphId = decoder.decode(fileName, model, name.replaceFirst("\\..*$", ""));
if (graphId < 0) {
VisualGraph.log.printError("[" + this.getClass().getName()
+ ".openGraph] [BAD] Cannot to add graph in model.");
throw new CoreException("Cannot to add graph in model.", EnumCriticalityException.WARNING);
}
openedFilesList.add(fileName);
VisualGraph.log.printInfo("Graph load time: " + (new Date().getTime() - d.getTime()) / 1000.0 + "sec");
return graphId;
}
public Set<String> getAvailableExtension() {
if (decoders != null)
return decoders.keySet();
else
return null;
}
public List<String> getOpenedFilesList() {
return openedFilesList;
}
}